// 根据某个参数查询树形将相同参数整合二维数组
// utils.js
export function groupTreeByProperty(nodes, propName, childrenKey = 'children') {
  const groups = new Map()

  function traverse(node) {
    if (!node) return
    // 收集当前节点属性
    if (node[propName] !== undefined) {
      const key = node[propName]
      if (!groups.has(key)) {
        groups.set(key, [])
      }
      groups.get(key).push(node)
    }

    // 递归处理子节点
    if (Array.isArray(node[childrenKey])) {
      node[childrenKey].forEach(child => traverse(child))
    }
  }

  // 遍历所有根节点
  nodes.forEach(root => traverse(root))

  // 转换为目标数组格式
  return Array.from(groups, ([propValue, nodes]) => ({
    [propName]: propValue,
    items: nodes
  }))
}